//
//  PhaserBullet.m
//  LearningCocos
//
//  Created by abyssos on 2014. 3. 8..
//  Copyright (c) 2014년 ainhan. All rights reserved.
//

#import "PhaserBullet.h"

@implementation PhaserBullet

@synthesize myDirection;
@synthesize firingAnim;
@synthesize travelingAnim;

- (void)dealloc {
    [travelingAnim release];
    [firingAnim release];
    
    [super dealloc];
}

- (void)changeState:(CharacterStates)newState {
    [self stopAllActions];
    id action = nil;
    characterState = newState;
    
    CCLOG(@"PHASER newState : %d, traveling : %d, deadState : %d", newState, kStateTraveling, kStateDead);
    
    switch (newState) {
        case kStateSpawning:
            CCLOG(@"Phaser -> Changed state to Spawning");
            firingAnim.restoreOriginalFrame = NO;
            action = [CCAnimate actionWithAnimation:firingAnim];
            break;
            
        case kStateTraveling:
            CCLOG(@"Phaser -> Changed state to Traveling");
            CGPoint endLocation;
            if (myDirection == kDirectionLeft) {
                CCLOG(@"Phaser direction LEFT");
                endLocation = ccp(-10.0f, [self position].y);
            } else {
                CCLOG(@"Phaser direction RIGHT");
                endLocation = ccp(screenSize.width + 24.0f, [self position].y);
            }
            
            [self runAction:[CCMoveTo actionWithDuration:2.0f position:endLocation]];
            
            travelingAnim.restoreOriginalFrame = NO;
            action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:travelingAnim]];
            break;
            
        case kStateDead:
            CCLOG(@"Phaser -> Changed State to dead");
            // Remove from parent
            [self setVisible:NO];
            [self removeFromParentAndCleanup:YES];
            break;
            
        default:
            break;
    }
    
    if(action != nil)
        [self runAction:action];
}

- (BOOL)isOutsideOfScreen {
    CGPoint currentSpritePosition = [self position];
    
    CCLOG(@"isOutsideOfScreen X:%f, Y:%f", currentSpritePosition.x, currentSpritePosition.y);
    CCLOG(@"isOutsideOfScreen Screen : %f x %f", screenSize.width, screenSize.height);
    
    if ((currentSpritePosition.x < 0.0f) || (currentSpritePosition.x > screenSize.width)) {
        [self changeState:kStateDead];
        return YES;
    }
    return  NO;
}

- (void)updateStateWithDeltaTime:(ccTime)deltaTime andListOfGameObjects:(CCArray *)listOfGameObjects {

    if ([self isOutsideOfScreen]) {
        return;
    }

    CCLOG(@"PHASER updateStateWithDeltaTime -1- actions : %d", [self numberOfRunningActions]);
    
    if ([self numberOfRunningActions] == 0) {
        CCLOG(@"PHASER updateStateWithDeltaTime -2-");
        if (characterState == kStateSpawning) {
            CCLOG(@"PHASER updateStateWithDeltaTime -3-");
            [self changeState:kStateTraveling];
            return;
        } else {
            CCLOG(@"PHASER updateStateWithDeltaTime -4-");
            [self changeState:kStateDead];
            return;
            // Should not do anything else from traveling
        }
    }
}

- (void)initAnimations {
    CCLOG(@"# [PhaserBullet] initAnimations..................");
    
    [self setFiringAnim:[self loadPlistForAnimationWithName:@"firingAnim" andClassName:NSStringFromClass([self class])]];
    [self setTravelingAnim:[self loadPlistForAnimationWithName:@"travelingAnim" andClassName:NSStringFromClass([self class])]];
}

- (id)initWithSpriteFrameName:(NSString *)spriteFrameName {
    if ((self = [super initWithSpriteFrameName:spriteFrameName])) {
        CCLOG(@"### PhaserBullet initialized");
        screenSize = [CCDirector sharedDirector].winSize;
        CCLOG(@"PHASER screenSize : %f x %f", screenSize.width, screenSize.height);
        [self initAnimations];
        gameObjectType = kEnemyTypePhaser;
    }
    
    return self;
}


@end



















